package com.cloud.DrugManagement.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.DrugManagement.enums.OrderState;
import com.cloud.DrugManagement.mapper.DrugsMapper;
import com.cloud.DrugManagement.mapper.OrderContentMapper;
import com.cloud.DrugManagement.mapper.OrderDetailsMapper;
import com.cloud.DrugManagement.pojo.Drugs;
import com.cloud.DrugManagement.pojo.OrderContent;
import com.cloud.DrugManagement.pojo.OrderDetails;
import com.cloud.DrugManagement.service.OrderContentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhozixiong
 * @date 2023/02/09
 */
@Service
@RequiredArgsConstructor
public class OrderContentServiceImpl extends ServiceImpl<OrderContentMapper, OrderContent> implements OrderContentService {

    private final static int SIZE = 10;

    private final OrderDetailsMapper orderDetailsMapper;
    private final DrugsMapper drugsMapper;

    @Override
    public Page<OrderContent> submitSearch(int pages, long oid) {
        Page page = new Page(pages, SIZE);
        return baseMapper.submitSearch(page, oid);
    }

    @Override
    public Page<OrderContent> notSubmitSearch(int pages, long oid) {
        Page page = new Page(pages, SIZE);
        OrderDetails orderDetails = orderDetailsMapper.searchAllByOid(oid);
        return baseMapper.notSubmitSearch(page, oid, orderDetails.getSid());
    }
    
    @Override
    public boolean updateQuantityByOidAndDid(int quantity, long oid, int did) {
        return baseMapper.updateQuantityByOidAndDid(quantity, oid, did) >= 1;
    }

    @Override
    public boolean updateOrderPrice(long oid) {
        //查询供应商信息
        OrderDetails orderDetails = orderDetailsMapper.searchAllByOid(oid);
        //查出订单中所有商品当前的订购价格
        List<OrderContent> contentList = baseMapper.searchOidDidPrice(oid, orderDetails.getSid());
        //记录当前价格
        for(OrderContent orderContent : contentList) {
            baseMapper.updatePriceByOidAndDid(oid, orderContent.getDid(), orderContent.getPrice());
        }
        return true;
    }

    @Override
    public boolean insert(OrderContent orderContent) {
        if (ifOrderState(orderContent.getOid())) {
            //查询商品信息
            Drugs drugs = drugsMapper.searchAllByDid(orderContent.getDid());
            orderContent.setDname(drugs.getDname());
            orderContent.setSpecifications(drugs.getSpecifications());
            orderContent.setPacking(drugs.getPacking());
            return baseMapper.insert(orderContent) >= 1;
        }else {
            return false;
        }
    }

    @Override
    public boolean delByOidAndDid(long oid, int did) {
        return baseMapper.delByOidAndDid(oid, did) >= 1;
    }

    private boolean ifOrderState(long oid) {
        OrderDetails orderDetails = orderDetailsMapper.searchAllByOid(oid);
        return OrderState.NOT_SUBMITTED.equals(orderDetails.getState());
    }
}
